package org.ycsoft.PathComputation;

import java.util.*;

/**
 * Represent a station in a city map. A stop is uniquely identified by its ID.
 * and it has a stop name (ex : "Chatelet")
 * @author allad
 *
 */
public class Stop implements MetroidEntity {
    
    private String stopName;
    private int stopId;
    
    private String coordinates;
    
    private static int idCounter = 1; //class wise variable used for ensuring unicity of ids
    
    private Set<Edge> edges = new HashSet<Edge>();
    
    
    /**
     * @param stopName Name of the stop 
     */
    public Stop(String stopName){
    	if(stopName != null){
	        this.stopName = stopName;
	        this.stopId = idCounter++;
    	}
    	else{
    		this.stopName = null;
    		this.stopId = -1; //for the null stop, the stopId is -1
    	}
    }
    
    
    /**
     * @return the station name
     */
    public String getStopName(){
        return stopName;
    }
    
    /**
     * @return the id that uniquely identifies this stop
     */
    public int getStopId(){
        return stopId;
    }
	
    /**
     * Sets this stop's name to a new string of characters
     * @param stopName the name to be used as the stop name
     */
    public void setStopName(String stopName){
        this.stopName = stopName;
    }

    /**
     * @return All outgoing edges from this stops
     */
    public Set<Edge> getEdges(){
        return edges;
    }
    
    /**
     * Add an outgoing edge to this stop
     * @param edge the edge to be added. the edge must depart from this stop otherwise it won't be added 
     *  
     */
    public void addEdge(Edge edge){
        if(edge != null && edge.getDeparture().equals(this)){
            edges.add(edge);
        }
    } 
    
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object o){
    	if(o == null){
    		return false;
    	}
    	if(o instanceof Stop){
    		Stop s = (Stop)o;
    		return (s.stopId == this.stopId);    			
    	}
    	else if(o instanceof PriorityQueueItem){
    		PriorityQueueItem pi = (PriorityQueueItem)o;
    		return this.equals(pi.getStop());
    	}
    	else return false;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString(){
    	return "Stop : "+stopName;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    public int hashCode(){
    	return stopId;
    }


	/* (non-Javadoc)
	 * @see org.ycsoft.PathComputation.MetroidEnt// TODO Auto-generated method stubity#toMetroid()
	 */
	public String toMetroid() {
		StringBuilder sb = new StringBuilder();
		sb.append("stop"+Commons.LINE_SEPARATOR);
		sb.append("id:"+stopId+Commons.LINE_SEPARATOR);
		sb.append("name:"+stopName+Commons.LINE_SEPARATOR);
		sb.append("coordinates:"+coordinates+Commons.LINE_SEPARATOR); //TODO coordinates
		sb.append("end"+Commons.LINE_SEPARATOR);
		return sb.toString();
	}
   
	
}